﻿package bdz.projects.slab.sections {
	import bdz.dms.vo.SectionVO;
	import bdz.projects.slab.core.SlabSection;
	import flash.events.Event;
	import as3.common.ui.menu.core.MenuComponent;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import as3.projects.slfi.renderers.MediaRenderer;
	import as3.common.ui.menu.TweenedGridList;
	import flash.events.Event;
	import bdz.common.utils.BitmapUtils;
	import flash.text.*;
	import flash.events.MouseEvent;
	import bdz.common.utils.TweenLite;
	import as3.common.ui.menu.events.MenuComponentEvent;
	import flash.utils.*;
	import as3.common.utils.TextAnimator;
	import fl.transitions.easing.*;
	import bdz.projects.slab.renderers.ParagraphText;
	import flash.geom.*;
	import bdz.common.utils.DateUtils;
	import bdz.common.display.DisplayLoader;
	import bdz.common.model.AppEnvironment;
	import flash.net.*;
	/**
	* ...
	* @author Default
	*/
	public class SlabAboutTheArtist extends SlabSection {
		private var artistVO:SectionVO;
		private var activeData:Array;
		private var list:TweenedGridList;
		private var arrLeft:SimpleButton;
		private var arrRight:SimpleButton;
		private var listCover:MovieClip;
		private var infoText:InfoText;
		private var galleryVO:SectionVO;
		private var header:GalleryTitle;
		private var thumb:DisplayLoader;
		private var thumbBG:MovieClip;
		private var pressButton:DownloadPress;
		private var isStatement:Boolean = false;
		function SlabAboutTheArtist() {
			
		}
		override protected function initializeComponent(evt:Event):void {
			activeData = aboutTextParser(!isStatement ? artistVO.getProperty("about_the_artist") : artistVO.getProperty("artist_statement"));
			listCover = new MovieClip();
			listCover.x = 12;
			listCover.y = 120;
			BitmapUtils.createBounds(listCover, 620, 300, 0);
			addChild(listCover);
			
			createList();
			super.initializeComponent(evt);
		}
		private function listReady(evt:*):void {
			setUpArrows();
			createExtraDetails();
		}
		private function getDateString(str:String, incyear:Boolean = true):String {
			var dte:Date = new Date(str);
			var dstr:String = "";
			if (DateUtils.getMonthString(dte.getMonth()) == null) {
				return null;
			}
			dstr += DateUtils.getMonthString(dte.getMonth()) + " ";
			dstr += dte.getDate() + " ";
			if (incyear) {
				dstr += dte.getFullYear();
			}
			return dstr;
		}
		
		private function createExtraDetails():void {
			infoText = new InfoText();
			infoText.description = !isStatement ? ("About the Artist: " + artistVO.section_name) : ("Artist Statement: " + artistVO.section_name);
			infoText.x = listCover.x;
			infoText.y = Math.ceil(listCover.y + listCover.height)-80;
			addChild(infoText);
			
			header = new GalleryTitle();
			if (getDateString(gallery.getProperty("start_date"), false) == null) {
				header.description = gallery.section_name;
			}else{
				header.description = gallery.section_name + " - " + getDateString(gallery.getProperty("start_date"), false) + " to " + getDateString(gallery.getProperty("end_date"));
			}
			header.y = infoText.y+infoText.height;
			header.x = listCover.x;
			addChild(header);
			if(!isStatement){
				if (artistVO.getProperty("press_release") && artistVO.getProperty("press_release") != "") {
					pressButton = new DownloadPress();
					pressButton.x = listCover.x+1;
					pressButton.y = Math.ceil(header.y + header.height);
					pressButton.addEventListener(MouseEvent.CLICK, downloadPressRelease);
					pressButton.alpha = 0;
					TweenLite.to(pressButton, .6, {ease:Strong.easeOut, alpha:1, delay:1 } );
					addChild(pressButton);
				}
			}
			thumbBG = new MovieClip();
			addChild(thumbBG);
			
			thumb = new DisplayLoader();
			thumb.addEventListener(Event.COMPLETE, thumbnailLoaded);
			thumb.source = AppEnvironment.getEnv().getSetting("baseURL")+artistVO.getProperty("thumbnail");
			thumb.visible = false;
			addChild(thumb);
		}
		private function downloadPressRelease(evt:MouseEvent):void {
			navigateToURL(new URLRequest(AppEnvironment.getEnv().getSetting("baseURL") + artistVO.getProperty("press_release")), "_blank");
		}
		private function thumbnailLoaded(evt:Event):void {
			
			BitmapUtils.createBounds(thumbBG, thumb.width + 10, thumb.height + 10, 1, 0x221e1f);
			thumb.alpha = thumbBG.alpha = 0;
			thumbBG.x = listCover.x + listCover.width - thumbBG.width
			thumbBG.x += 5;
			thumbBG.y = infoText.y;
			thumb.x = thumbBG.x + 5;
			thumb.y = thumbBG.y + 5;
			thumb.visible = true;
			TweenLite.to(thumb, .6, {delay:.1, alpha:1, ease:Strong.easeOut } );
			TweenLite.to(thumbBG, .6, { alpha:1, ease:Strong.easeOut } );
		}
		private function setUpArrows():void {
			if(list.maxhscroll){
				arrLeft = new arrowLeft();
				arrRight = new arrowRight();
				arrLeft.x = listCover.x-30;
				arrRight.x = listCover.x + listCover.width+30;
				arrLeft.y = arrRight.y = listCover.y+(listCover.height/2)-80;
				arrLeft.visible = false;
				arrRight.alpha = arrLeft.alpha = 0;
				TweenLite.to(arrRight, .8, {alpha:1});
				TweenLite.to(arrLeft, .8, {alpha:1});
				
				addChild(arrLeft);
				addChild(arrRight);
				arrRight.addEventListener(MouseEvent.CLICK, scrollRight);
				arrLeft.addEventListener(MouseEvent.CLICK, scrollLeft);
			}
		}
		private function scrollRight(evt:MouseEvent):void{
			list.scrollLeft();
			if(list.curhscroll == list.maxhscroll){
				arrRight.visible = false;
			}
			if(list.curhscroll){
				arrLeft.visible = true;
			}
		}
		private function scrollLeft(evt:MouseEvent):void{
			list.scrollRight();
			if(list.curhscroll < list.maxhscroll){
				arrRight.visible = true;
			}
			if(!list.curhscroll){
				arrLeft.visible = false;
			}
		}
		private function createList():void {
			var tmp:ParagraphText = new ParagraphText(new Object(), new TextFormat());
			list = new TweenedGridList("bdz.projects.slab.renderers.ParagraphText");
			list.addEventListener("listReady", listReady);
			
			list.y = listCover.y;
			list.x = listCover.x;
			list.xpad = 0;
			list.ypad = 10;
			list.labelField = "_";
			list.viewableColumns = 1;
			list.maxrows = 1;
			list.scrollDuration = .7;
			list.delay = .01;
			list.edgeDelay = .03;
			list.offsetEdges = false;
			list.arrayItems = activeData;
			list.mask = listCover;
			list.easing = Strong.easeInOut;
			addChild(list);
		}
		private function aboutTextParser(str:String):Array{
			var ret:Array = new Array();
			var strs:Array = escape(str).split("%0A%0A");
			
			for(var i:Number = 0;i<strs.length;i++){
				if(strs[i] != ""){
					ret.push({id:i,paragraph:unescape(strs[i]+"%0A%0A")});
				}
			}
			return ret;
		}
		public function set forStatement(b:Boolean):void {
			isStatement = b;
		}
		public function get forStatement():Boolean {
			return isStatement;
		}
		public function set gallery(svo:SectionVO):void {
			galleryVO = svo;
		}
		public function get gallery():SectionVO {
			return galleryVO;
		}
		public function set artist(svo:SectionVO):void {
			artistVO = svo;
		}
		public function get artist():SectionVO {
			return artistVO;
		}
	}
	
}